package com.miplus.ccrm.core.service.conf;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.conf.MsgTmpResVo;
import com.miplus.ccrm.core.controller.vo.conf.OutGrpEditVo;
import com.miplus.ccrm.core.controller.vo.conf.OutGrpReqVo;
import com.miplus.ccrm.core.controller.vo.conf.OutGrpResVo;
import com.miplus.ccrm.core.pojo.*;
import com.miplus.ccrm.core.mapper.SysOutsourcingMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author pc
* @description 针对表【sys_outsourcing】的数据库操作Service实现
* @createDate 2024-03-23 18:19:11
*/
@Service
public class SysOutsourcingServiceImpl extends MPJBaseServiceImpl<SysOutsourcingMapper, SysOutsourcing>
    implements SysOutsourcingService{

    @Resource
    private SysGrpService sysGrpService;

    @Override
    public IPage<OutGrpResVo> outSourcingList(String $usr, OutGrpReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                OutGrpResVo.class,
                new MPJLambdaWrapper<SysOutsourcing>()
                        .selectAll(SysOutsourcing.class)
                        .like(JPubUtils.IsNotEmpty(paras.getGrpname()),"grpname",paras.getGrpname())
                        .eq(paras.getStatus()!=null,"status",paras.getStatus())
                        .eq(JPubUtils.IsNotEmpty(paras.getGrpid()),"grpid",paras.getGrpid())
                        .between(JPubUtils.IsNotEmpty(paras.getStartdate()), SysOutsourcing::getCrdt,JPubUtils.StringToDate(paras.getStartdate()),JPubUtils.StringToDate(paras.getEnddate()))
        );
    }

    @Override
    public CmdResVo outSourcingUpdate(String $usr, OutGrpEditVo paras)
    {
        SysOutsourcing def=new SysOutsourcing();
        BeanUtils.copyProperties(paras,def);
        if(JPubUtils.IsNotEmpty(paras.getStartdt())){
            def.setStartdt(JPubUtils.StringToDate(paras.getStartdt()));
        }
        if(JPubUtils.IsNotEmpty(paras.getEnddt())){
            def.setEnddt(JPubUtils.StringToDate(paras.getEnddt()));
        }
        def.setMddt(new Date());
        def.setMdusr($usr);
        int ret= baseMapper.updateById(def);
        UpToLinkTable($usr,paras);
        return new CmdResVo(ret,def.getGrpid());
    }

    private void UpToLinkTable(String $usr,OutGrpEditVo paras)
    {
        SysGrp grp=sysGrpService.getById(paras.getGrpid());
        if(grp==null){
            grp=new SysGrp();
            grp.setStatus(paras.getStatus().toString());
            grp.setGrpid(paras.getGrpid());
            grp.setGrpname(paras.getGrpname());
            grp.setType(5); //委外机构
            grp.setMddt(new Date());
            grp.setMdusr($usr);
            grp.setCrdt(new Date());
            grp.setCrusr($usr);
            sysGrpService.getBaseMapper().insert(grp);
        }else{
            grp.setGrpname(paras.getGrpname());
            grp.setStatus(paras.getStatus().toString());
            grp.setMddt(new Date());
            grp.setMdusr($usr);
            sysGrpService.getBaseMapper().updateById(grp);
        }
    }

    @Override
    public CmdResVo outSourcingInsert(String $usr,OutGrpEditVo paras)
    {
        SysOutsourcing def=new SysOutsourcing();
        BeanUtils.copyProperties(paras,def);
        if(JPubUtils.IsNotEmpty(paras.getStartdt())){
            def.setStartdt(JPubUtils.StringToDate(paras.getStartdt()));
        }
        if(JPubUtils.IsNotEmpty(paras.getEnddt())){
            def.setEnddt(JPubUtils.StringToDate(paras.getEnddt()));
        }
        def.setMddt(new Date());
        def.setMdusr($usr);
        def.setCrdt(new Date());
        def.setCrusr($usr);
        int ret= baseMapper.insert(def);

        UpToLinkTable($usr,paras);

        return new CmdResVo(ret,def.getGrpid());
    }

    @Override
    public CmdResVo outSourcingDelete(String id)
    {
        int ret= baseMapper.deleteById(id);
        sysGrpService.remove(new QueryWrapper<SysGrp>().eq("parentid",id));
        sysGrpService.removeById(id);

        return new CmdResVo(ret,id);
    }

    @Override
    public CmdResVo outSourcingSetStatus(String $usr,String id,Integer status)
    {
        SysOutsourcing def=new SysOutsourcing();
        def.setGrpid(id);
        def.setStatus(status);
        def.setMddt(new Date());
        def.setMdusr($usr);
        int ret= baseMapper.updateById(def);
        SysGrp grp=sysGrpService.getById(id);
        if(grp!=null){
            grp.setGrpid(id);
            grp.setStatus(status.toString());
            grp.setMddt(new Date());
            grp.setMdusr($usr);
            sysGrpService.getBaseMapper().updateById(grp);
        }
        return new CmdResVo(ret,def.getGrpid());
    }
}




